package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.dao.UserInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.UserInfoMapperImpl;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.model.UserSessionInfo;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.base.Header;
import cc.rengu.igas.bsps.facade.request.TransPasswordSettingRequest;
import cc.rengu.igas.bsps.facade.response.TransPasswordSettingResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.PinTypeEnum;
import cc.rengu.oltp.service.common.enums.SmsTypeEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.PasswordInfo;
import cc.rengu.oltp.service.realize.SafeKeyBoardService;
import cc.rengu.oltp.service.realize.SmsService;
import cc.rengu.oltp.service.realize.impl.SafeKeyBoardServiceImpl;
import cc.rengu.oltp.service.realize.impl.SmsServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 交易密码设置服务
 */

public class TransPasswordSettingService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        TransPasswordSettingRequest transPasswordSettingRequest = new TransPasswordSettingRequest();
        ConvertUtil.convertOutput(transPasswordSettingRequest);
        return transPasswordSettingRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        TransPasswordSettingRequest transPasswordSettingRequest = (TransPasswordSettingRequest) request;
        TransPasswordSettingResponse transPasswordSettingResponse = new TransPasswordSettingResponse();
        // 法人行号，用户标识，短信验证码，短信唯一标识，交易密码获取
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String userId = transPasswordSettingRequest.getUserId();
        String smsCode = transPasswordSettingRequest.getSmsCode();
        String smsAuthId = transPasswordSettingRequest.getSmsAuthId();
        String transPassword = transPasswordSettingRequest.getTransPassword();
        String chanlId = transPasswordSettingRequest.getHeader().getChanlId();
        String session = transPasswordSettingRequest.getHeader().getSession();
        rglog.info("法人行号:" + instId + "，用户标识:" + userId + "，短信验证码:" + smsCode + "，短信唯一标识" + smsAuthId);

        /*2-验证短信验证码*/
        SmsService smsService = new SmsServiceImpl();
        boolean smsCheckStatus = smsService.verifySmsCode(instId, SmsTypeEnum.SET_TRANS_PASSWORD.getSmsType(), smsCode, smsAuthId);
        if (!smsCheckStatus) {
            rglog.error("短信验证码校验失败！");
            throw new BizException(OltpRpcdEnum.VERIFY_SMS_CODE_ERROR.getRespCode(), OltpRpcdEnum.VERIFY_SMS_CODE_ERROR.getRespDesc());
        } else {
            PasswordInfo passwordInfo = new PasswordInfo(transPasswordSettingRequest.getMchntNo(), PinTypeEnum.DIGITAL, transPassword);
            UserInfoMapper userInfoMapper = new UserInfoMapperImpl();
            SafeKeyBoardService safeKeyBoardService = new SafeKeyBoardServiceImpl();
            if (chanlId.equals("PCWEB")) {
                Header header = transPasswordSettingRequest.getHeader();
                UserSessionInfo keySessionInfo = JSON.parseObject(RedisUtil.onceGet(header.getSession()), UserSessionInfo.class);
                keySessionInfo.setRandomString("");
                RedisUtil.onceSet(header.getSession(), JSONObject.toJSONString(keySessionInfo), BspsAppParamConstant.SESSION_EFFECTIVE_TIME);

                transPassword = safeKeyBoardService.encryptPassword(instId, null, passwordInfo, null);
            } else {
                transPassword = safeKeyBoardService.encryptPassword(instId, null, passwordInfo, null);
            }

            int iReturnCode = userInfoMapper.updateBspUserTransPasswordByPrimaryKey(instId, userId, transPassword);
            if (0 != iReturnCode) {
                rglog.error("更新用户交易密码信息失败!");
                throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        /* 重组返回结果 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(transPasswordSettingResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        TransPasswordSettingResponse transPasswordSettingResponse = (TransPasswordSettingResponse) bizResponse.getResult();
        ConvertUtil.convertInput(transPasswordSettingResponse);
    }
}
